package Tree;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-29  22:11
 * @Description: TODO
 * @Version: 1.0
 */
public class 二叉搜索树的累加538 {

    // 中序遍历： 左中右 == 右中左
    int pre = 0;

    public TreeNode convertBST(TreeNode root) {
        if (root == null) {
            return null;
        }
        traversal(root);
        return root;
    }

    // 执行的右中左做的逻辑顺序 倒叙的顺序
    private void traversal(TreeNode root) {
        if (root == null) {
            return;
        }
        // 右
        traversal(root.right);
        // 中
        root.val += pre;
        pre = root.val;
        // 左
        traversal(root.left);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
